/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaUsuarios.java
 *
 * Created on 01-jun-2012, 22:30:38
 */

package edu.eam.gruposenior.gestorLEcciones.vista;

import javax.swing.JOptionPane;

import edu.eam.gruposenior.gestorLEcciones.controladores.UsuarioController;
import edu.eam.gruposenior.gestorLEcciones.entidades.Usuario;

/**
 * 
 * @author Claudia
 */
public class VentanaUsuarios extends javax.swing.JFrame {

	private UsuarioController control;

	/** Creates new form VentanaUsuarios */
	public VentanaUsuarios() {
		initComponents();
		control = new UsuarioController();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bEditar = new javax.swing.JButton();
        tfTel = new javax.swing.JTextField();
        bBuscar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        bCrear = new javax.swing.JButton();
        tfNick = new javax.swing.JTextField();
        tfConPass = new javax.swing.JPasswordField();
        tfPassword = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tfNombreCom = new javax.swing.JTextField();
        tfCedula = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        bEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Notepad.png"))); // NOI18N
        bEditar.setText("Editar");
        bEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEditarActionPerformed(evt);
            }
        });

        bBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Search.png"))); // NOI18N
        bBuscar.setText("Buscar");
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Telefono");

        bCrear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Add.png"))); // NOI18N
        bCrear.setText("Crear");
        bCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCrearActionPerformed(evt);
            }
        });

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel6.setText("Confirmacion Password");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setText("Nombre Completo");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel4.setText("NickName");

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel5.setText("Password");

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText("Cedula");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(55, 55, 55)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(layout.createSequentialGroup()
                        .add(jLabel5)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(tfPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 311, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(jLabel4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(tfNick, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 311, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(tfTel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 311, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(tfCedula, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 311, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(tfNombreCom, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 311, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(jLabel6)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(tfConPass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 311, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                .add(bCrear)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(bEditar)))))
                .add(18, 18, 18)
                .add(bBuscar)
                .addContainerGap(45, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(47, 47, 47)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(tfCedula, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1)
                    .add(bBuscar))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(tfNombreCom, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(tfTel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(tfNick, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(tfPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(tfConPass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel6))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(bCrear)
                    .add(bEditar))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void bCrearActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_bCrearActionPerformed
		String pass = new String(tfPassword.getPassword());
		String passCon = new String(tfConPass.getPassword());
		if (pass.equals(passCon)) {
			Usuario usuario = control.buscarUsuario(tfCedula.getText());

			if (usuario == null) {

				if (!tfCedula.getText().equals("")
						&& !tfNick.getText().equals("")
						&& !tfNombreCom.getText().equals("")
						&& !pass.equals("") && !tfTel.getText().equals("")
						&& !passCon.equals("")) {

					control.crearUsuario(tfCedula.getText(), true,
							tfNick.getText(), tfNombreCom.getText(), pass,
							tfTel.getText());
					tfCedula.setText("");
					tfConPass.setText("");
					tfNick.setText("");
					tfNombreCom.setText("");
					tfPassword.setText("");
					tfTel.setText("");
					JOptionPane.showMessageDialog(null,
							"Usuario creado con exito");
				} else {
					JOptionPane.showMessageDialog(null,
							"Hay campos que no tienen valor", "Campos vacios",
							JOptionPane.ERROR_MESSAGE);
				}

			} else {
				JOptionPane.showMessageDialog(null, "El usuario ya existe",
						"Usuario Existente", JOptionPane.ERROR_MESSAGE);
			}

		} else {
			JOptionPane.showMessageDialog(null, "El password no coincide",
					"Password Incorrecto", JOptionPane.ERROR_MESSAGE);
		}

	}// GEN-LAST:event_bCrearActionPerformed

	private void bEditarActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_bEditarActionPerformed
		String pass = new String(tfPassword.getPassword());
		String passCon = new String(tfConPass.getPassword());
		if (pass.equals(passCon)) {

			try {

				if (!tfCedula.getText().equals("")
						&& !tfNick.getText().equals("")
						&& !tfNombreCom.getText().equals("")
						&& !pass.equals("") && !tfTel.getText().equals("")
						&& !passCon.equals("")) {

					control.editarUsuario(tfCedula.getText(), true,
							tfNick.getText(), tfNombreCom.getText(), pass,
							tfTel.getText());

					tfCedula.setText("");
					tfConPass.setText("");
					tfNick.setText("");
					tfNombreCom.setText("");
					tfPassword.setText("");
					tfTel.setText("");
					JOptionPane.showMessageDialog(null,
							"Usuario editado con exito");
				} else {
					JOptionPane.showMessageDialog(null,
							"Hay campos que no tienen valor", "Campos vacios",
							JOptionPane.ERROR_MESSAGE);
				}

			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, e.getMessage(),
						"Usuario no Encontrado", JOptionPane.ERROR_MESSAGE);
			}

		} else {
			JOptionPane.showMessageDialog(null, "El password no coincide",
					"Password Incorrecto", JOptionPane.ERROR_MESSAGE);
		}
	}// GEN-LAST:event_bEditarActionPerformed

	private void bEliminarActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_bEliminarActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_bEliminarActionPerformed

	private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_bBuscarActionPerformed
		Usuario usuario = control.buscarUsuario(tfCedula.getText());

		if (usuario != null) {
			tfNombreCom.setText(usuario.getNombreCompleto());
			tfNick.setText(usuario.getNickname());
			tfTel.setText(usuario.getTelefono());
			tfPassword.setText(usuario.getPassword());
			tfConPass.setText(usuario.getPassword());

		} else {
			JOptionPane.showMessageDialog(null, "El usuario no existe");
		}
	}// GEN-LAST:event_bBuscarActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new VentanaUsuarios().setVisible(true);
			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bCrear;
    private javax.swing.JButton bEditar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField tfCedula;
    private javax.swing.JPasswordField tfConPass;
    private javax.swing.JTextField tfNick;
    private javax.swing.JTextField tfNombreCom;
    private javax.swing.JPasswordField tfPassword;
    private javax.swing.JTextField tfTel;
    // End of variables declaration//GEN-END:variables

}
